package com.slg.android.manyanime;


import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.adapter.CartoonFragmentAdapter;


public class CartoonActivity extends FragmentActivity implements
        RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener, View.OnClickListener {
    @ViewInject(R.id.cartoon_title_back_)
    ImageView backButton;

    @ViewInject(R.id.cartoon_title_search)
    ImageView searchButton;

    @ViewInject(R.id.cartoon_fragment_viewpager)
    ViewPager viewPager;

    @ViewInject(R.id.cartoon_navigation_radioGroup)
    RadioGroup navigationBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_cartoon_entry);
        ViewUtils.inject(this);
        CartoonFragmentAdapter adapter = new CartoonFragmentAdapter(getSupportFragmentManager());

        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(2);
        viewPager.setOnPageChangeListener(this);
        navigationBar.setOnCheckedChangeListener(this);
        if (backButton != null) {
            backButton.setOnClickListener(this);
        }
        if (searchButton != null) {
            searchButton.setOnClickListener(this);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewPager.removeAllViews();
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        for (int i = 0; i < group.getChildCount(); i++) {
            RadioButton childRadioButton = (RadioButton) navigationBar.getChildAt(i);
            if (childRadioButton.isChecked()) {
                childRadioButton.setTextColor(Color.WHITE);
                childRadioButton.setBackgroundDrawable(
                        getResources().getDrawable(R.drawable.navigation_bar_item_checked_bg));
                viewPager.setCurrentItem(i);
            } else {
                childRadioButton.setTextColor(getResources().getColor(R.color.main_menu_text));
                childRadioButton.setBackgroundResource(R.drawable.navigation_bar_item_bg);
            }

        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (navigationBar != null) {
            RadioButton childRadioButton = (RadioButton) navigationBar.getChildAt(position);
            childRadioButton.setChecked(true);
        }


    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.cartoon_title_back_:
                finish();
                break;

        }
    }
}
